import { request } from '@/utils/request';
import type {
  ReferrerListResponse,
  ReferrerDetailResponse,
  ReferralSettingsResponse,
  ReferralStats,
  ReferralSearchParams,
  ReferralSettingsUpdateData,
  UserReferralSettingsData,
  RemoveReferralRequest,
  RemoveReferralResponse
} from '@/types/referral';

export class ReferralService {
  // 추천인 목록 조회
  static async getReferrerList(params: ReferralSearchParams): Promise<ReferrerListResponse> {
    const response = await request.get('/api/admin/referral/list', { params });
    return response.data;
  }

  // 추천인 상세 조회
  static async getReferrerDetail(id: number, params?: { page?: number; limit?: number }): Promise<ReferrerDetailResponse> {
    const response = await request.get(`/api/admin/referral/${id}`, { params });
    return response.data;
  }

  // 추천 설정 조회
  static async getReferralSettings(): Promise<ReferralSettingsResponse> {
    const response = await request.get('/api/admin/referral/settings');
    return response.data;
  }

  // 전역 추천 설정 업데이트
  static async updateGlobalSettings(data: ReferralSettingsUpdateData): Promise<void> {
    await request.put('/api/admin/referral/settings/global', data);
  }

  // 특정 사용자 추천 비율 설정
  static async setUserSettings(data: UserReferralSettingsData): Promise<void> {
    await request.post('/api/admin/referral/settings/user', data);
  }

  // 사용자별 설정 삭제
  static async deleteUserSettings(userId: number): Promise<void> {
    await request.delete(`/api/admin/referral/settings/user/${userId}`);
  }

  // 추천 통계 데이터 조회
  static async getReferralStats(): Promise<ReferralStats> {
    const response = await request.get('/api/admin/referral/stats');
    return response.data;
  }

  // 解除推荐关系
  static async removeReferralRelation(data: RemoveReferralRequest): Promise<RemoveReferralResponse> {
    const response = await request.post('/api/admin/referral/remove-relation', data);
    return response.data;
  }

  // 获取推荐收益提现列表
  static async getReferralWithdrawList(params: any): Promise<any> {
    const response = await request.get('/api/admin/referral/withdraw/list', { params });
    return response.data;
  }

  // 获取推荐收益提现详情
  static async getReferralWithdrawDetail(id: number): Promise<any> {
    const response = await request.get(`/api/admin/referral/withdraw/${id}`);
    return response.data;
  }

  // 处理推荐收益提现申请
  static async processReferralWithdraw(id: number, data: { action: 'approve' | 'reject'; note?: string }): Promise<any> {
    const response = await request.post(`/api/admin/referral/withdraw/${id}/process`, data);
    return response.data;
  }
}

// 导出adminApi对象以便在组件中使用
export const adminApi = {
  getReferralWithdrawList: ReferralService.getReferralWithdrawList,
  getReferralWithdrawDetail: ReferralService.getReferralWithdrawDetail,
  processReferralWithdraw: ReferralService.processReferralWithdraw
}